package property.controller;

import com.alibaba.fastjson.JSONObject;
import property.pojo.*;
import property.service.FeedbackDisposeService;
import property.service.impl.FeedbackDisposeServiceImpl;
import property.utils.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/FeedbackDispose/*"})
public class FeedbackDisposeServlet extends BaseServlet {
    FeedbackDisposeService feedbackDisposeService = new FeedbackDisposeServiceImpl();

    public void addFeedbackDispose(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();


        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        if(character!=1){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }
        Staff s = (Staff) session.getAttribute("currentUser");
 
        FeedbackDispose feedbackDispose = JSONObject.toJavaObject(jb.getJSONObject("insert"),FeedbackDispose.class);
        feedbackDispose.setFdStaffId(s.getsId());
        int i= feedbackDisposeService.addFeedbackDispose(feedbackDispose);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "接取反馈工作成功！");
        }else if (i==-2){
            jbReturn.put("code", 0);
            jbReturn.put("messages", "已接过该反馈处理或不存在该反馈！");
        } else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void deleteFeedbackDispose(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();


        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");

        if(character!=2){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        FeedbackDisposeKey feedbackDisposeKey = JSONObject.toJavaObject(jb,FeedbackDisposeKey.class);
        if(((Staff)session.getAttribute("currentUser")).getsId() != feedbackDisposeKey.getFdStaffId()){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        int i = feedbackDisposeService.deleteFeedbackDispose(feedbackDisposeKey);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "删除反馈处理记录成功");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void updateFeedbackDispose(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");

        if(character!=1){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        FeedbackDispose feedbackDispose = JSONObject.toJavaObject(jb.getJSONObject("update"),FeedbackDispose.class);
        if(((Staff)session.getAttribute("currentUser")).getsId() != feedbackDispose.getFdStaffId()){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }
        int i = feedbackDisposeService.updateFeedbackDispose(feedbackDispose);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "修改反馈处理记录成功");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }
}
